import Vue from 'vue'
import VueRouter from 'vue-router'
import {	Message,MessageBox} from 'element-ui'
import store from '@/vuex'



import axios from 'axios'
Vue.prototype.$http = axios;
Vue.prototype.$http.defaults.withCredentials=true;

Vue.prototype.$http.interceptors.response.use(response => {
  if (response.data.code== "401") {
    localStorage.clear();	
	MessageBox.alert(response.data.message);
    router.push({
      name:'Login'
    })
  }
  return response;
}, error => {
  if (error && error.response) {
    switch (error.response.status) {
      case 404:
        router.push({name:'error-404'});
        // error.message = '请求出错(404)'
        break;
      
      case 500:
        router.push({ name:'error-500'});
        //  error.message = '服务器错误(500)';
        break;
     
      default: error.message = `连接出错(${error.response.status})!`;
    }
  }
  return Promise.reject(error);
});






const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err.message)
}



Vue.use(VueRouter)

const routes = [{
	path: '/',
	name:'首页',
	component: () => import('../views/Home.vue'),
	children: [
		{
			path: '/role',
			name:'角色管理',
			component: () => import('../views/role/role.vue')
		},{
			path: '/permission',
			name:'权限',
			component: () => import('../views/permission/permission.vue')
		},{
			path: '/role-permission',
			name:'role-permission',
			component: () => import('../views/role/role-permission.vue')
		},{
			path: '/user',
			name:'用户',
			component: () => import('../views/user/user.vue')
		},{
			path: '/territory',
			name:'我的地盘',
			component: () => import('../views/territory/territory.vue')
		},{
			path: '/calendar',
			name:'日程表',
			component: () => import('../views/calendar/calendar.vue')
		},{
			path: '/task',
			name:'任务',
			component: () => import('../views/task/task.vue')
		},{
			path: '/taskBoard',
			name:'任务看板',
			component: () => import('../views/taskBoard/taskBoard.vue')
		},{
			path: '/team',
			name:'团队',
			component: () => import('../views/team/team.vue')
		},{
			path: '/teamUser',
			name:'团队人员',
			component: () => import('../views/team/team-user.vue')
		},{
			path: '/project',
			name:'项目',
			component: () => import('../views/project/project.vue')
		},{
			path: '/student',
			name:'学生管理',
			component: () => import('../views/student/studentManage/index.vue')
		},{
			path: '/studentSource',
			name:'课程视频',
			component: () => import('../views/student/project-class/projectClass.vue')
		},{
			path: '/class',
			name:'班级管理',
			component: () => import('../views/student/class/index.vue')
		},{
			path: '/courseList',
			name:'课程列表',
			component: () => import('../views/student/course/courseList.vue')
		},{
			path: '/courseInfo',
			name:'发布课程',
			component: () => import('../views/student/course/courseInfo.vue')
		},{
			path: '/course/courseContent',
			name:'发布课程目录',
			component: () => import('../views/student/course/courseContent.vue')
		},{
			path: '/overview',
			name:'迭代概览',
			component: () => import('../views/student/overview/overview.vue')
		},{
			path: '/generalOverview',
			name:'项目和任务概览',
			component: () => import('../views/generalOverview/index.vue')
		},{
			path: '/ClassesSpace',
			name:'课程详情',
			component: () => import('../views/student/class/ClassesSpace.vue')
		},{
			path: '/testSituation',
			name:'考试情况',
			component: () => import('../views/student/class/TestSituation.vue')
		},{
			path: '/markTestPaper',
			name:'考试信息',
			component: () => import('../views/student/exam/markTestPaper.vue')
		},{
			path: '/mytest',
			name:'我的试卷',
			component: () => import('../views/student/exam/myTest.vue')
		},{
			path: '/markTestPaper',
			name:'试卷信息',
			component: () => import('../views/student/exam/markTestPaper.vue')
		},{
			path: '/topicBank',
			name:'题库',
			component: () => import('../views/student/topicBank/index.vue')
		},{
			path: '/topicBank/content',
			name:'题库内容',
			component: () => import('../views/student/topicBank/content.vue')
		},{
			path: '/course/documents',
			name:'文件列表',
			component: () => import('../views/student/course/courseDocument.vue')
		},,{
			path: '/course/notice',
			name:'通知列表',
			component: () => import('../views/student/course/courseNotice.vue')
		},{
			path: '/message/messageList',
			name:'我的消息',
			component: () => import('../views/message/myMessage.vue')
		}
		
		
		
	]
},{
	path:'/login',
	name:'Login',
	component: () => import('../views/login/login.vue'),
 },{
	path:'/studentClient',
	name:'studentClient',
	component: () => import('../views/client/studentClientIndex/index.vue'),
 },{
	path:'/mycourse',
	name:'mycourse',
	component: () => import('../views/client/course/index.vue'),
 },{
	path:'/course/video',
	name:'video',
	component: () => import('../views/client/video/index.vue'),
 },{
			path: '/editTest',
			name:'editTest',
			component: () => import('../views/student/exam/editTest.vue')
		}
]


const router = new VueRouter({
	mode: 'hash',
	base: process.env.BASE_URL,
	routes
})

const whitelist = ['/login']


router.beforeEach((to, from, next) => {
   //在白名单里面，直接放行
  if(whitelist.indexOf(to.path) !== -1){
	  next(); 
  }else{
	  //是否是登录的状态
	  let user = store.getters.getToken;
	  if(user){
		 next();//放行  
	  }else{   //跳转到登录
		 next("/login");  
	  }
	  //to-login
  }
 
  
 
  
})


export default router
